<?php

class Poll {
	private $ID = null;
	private $Question;
	private $StartedOn;
	private $ExpiresOn;
	private $Active;
	private $ShowResults;
	private $MembersOnly;
	private $PollItems = array();

	private $Controller;

	function __construct($id = null, $Controller){
		$this->Controller = $Controller;

		if($id == null){
			return true;
		}

		$MySQL = $this->Controller->UseApps["mysql"];

		$PollQuery = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}polls WHERE id = $id");

		if(mysql_num_rows($PollQuery) <= 0){
			return false;
		}

		$Fetch = mysql_fetch_object($PollQuery);

		$this->ID = $Fetch->id;
		$this->Question = $Fetch->question;
		$this->StartedOn = $Fetch->started_on;
		$this->ExpiresOn = $Fetch->expires_on;
		$this->Active = ($this->ExpiresOn <= time()) ? false : (($Fetch->active == "true") ? true : false);
		$this->ShowResults = ($Fetch->show_results == "true") ? true : false;
		$this->MembersOnly = ($Fetch->members_only == "true") ? true : false;

		$ItemsQuery = $MySQL->query("SELECT id FROM {$MySQL->getPrefix()}poll_items WHERE poll_id = $this->ID ORDER BY id ASC");

		if(mysql_num_rows($ItemsQuery) <= 0){
			return true;
		}

		while($item = mysql_fetch_object($ItemsQuery)){
			$this->PollItems[] = new PollItem($item->id, $this->Controller);
		}
	}

	function update(){
		if(empty($this->Question)){
			return false;
		}

		$MySQL = $this->Controller->UseApps["mysql"];

		$Active = ($this->Active == true) ? "true" : "false";
		$ShowResults = ($this->ShowResults == true) ? "true" : "false";
		$MembersOnly = ($this->MembersOnly == true) ? "true" : "false";

		if($this->ID == null){
			if(empty($this->StartedOn)){
				$this->StartedOn = time();
			}

			$MySQL->query("INSERT INTO {$MySQL->getPrefix()}polls (question, started_on, expires_on, active, show_results, members_only) VALUES('$this->Question', '$this->StartedOn', '$this->ExpiresOn', '$Active', '$ShowResults', '$MembersOnly')");
			$this->ID = mysql_insert_id();
			return true;
		}
		else {
			$MySQL->query("UPDATE {$MySQL->getPrefix()}polls SET question = '$this->Question', expires_on = '$this->ExpiresOn', active = '$Active', show_results = '$ShowResults', members_only = '$MembersOnly' WHERE id = $this->ID");
			return true;
		}
	}

	function delete(){
		if($this->ID == null){
			return false;
		}

		$MySQL = $this->Controller->UseApps["mysql"];

		$MySQL->query("DELETE FROM {$MySQL->getPrefix()}polls WHERE id = $this->ID");

		foreach($this->PollItems as $item){
			$item->delete();
		}

		return true;
	}

	function getID(){
		return $this->ID;
	}

	function getQuestion(){
		return $this->Question;
	}

	function getStartedOn(){
		return $this->StartedOn;
	}

	function getStartDate($format){
		return date($format, $this->StartedOn);
	}

	function getExpiresOn(){
		return $this->ExpiresOn;
	}

	function getExpireDate($format){
		return date($format, $this->ExpiresOn);
	}

	function getPollItems(){
		return $this->PollItems;
	}

	function isActive(){
		return $this->Active;
	}

	function canShowResults(){
		return $this->ShowResults;
	}

	function membersOnly(){
		return $this->MembersOnly;
	}

	function getItemPercentage(PollItem $Item){
		$Percent = ($Item->getVotes() == 0) ? 0 : ($Item->getVotes()/$this->getTotalVotes()*100);

		return round($Percent, 2);
	}

	function getTotalVotes(){
		$NumVotes = 0;

		foreach($this->PollItems as $Item){
			$NumVotes += $Item->getVotes();
		}

		return $NumVotes;
	}

	function setQuestion($question){
		$this->Question = $question;
		return true;
	}

	function setExpiresOn($timestamp){
		$this->ExpiresOn = $timestamp;
		return true;
	}

	function setActive($is_active){
		$this->Active = $is_active;
		return true;
	}

	function setShowResults($show_results){
		$this->ShowResults = $show_results;
		return true;
	}

	function setMembersOnly($members_only){
		$this->MembersOnly = $members_only;
		return true;
	}
}

?>